package dto;

// Generated May 6, 2011 4:19:20 PM by Hibernate Tools 3.3.0.GA
import org.hibernate.criterion.Order;
import SessionFactory.SessionFactoryUtil;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import static org.hibernate.criterion.Example.create;

/**
 * Home object for domain model class Sections.
 * @see dto.Sections
 * @author Hibernate Tools
 */
public class SectionsHome {

    private static final Log log = LogFactory.getLog(SectionsHome.class);
    private Vector<Sections> innerSections = new Vector<Sections>();    // to store the sections(subSections) inside a special section
    private Vector<Topics> innerTopics = new Vector<Topics>();    // to store the topics inside a special section
    private final SessionFactory sessionFactory = SessionFactoryUtil.getInstance();
    private Sections mainForum;    // to store the parent node

    public void persist(Sections transientInstance) {
        log.debug("persisting Sections instance");
        try {
            sessionFactory.getCurrentSession().persist(transientInstance);
            log.debug("persist successful");
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

    public void attachDirty(Sections instance) {
        log.debug("attaching dirty Sections instance");
        try {
            sessionFactory.getCurrentSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(Sections instance) {
        log.debug("attaching clean Sections instance");
        try {
            sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void delete(Sections persistentInstance) {
        log.debug("deleting Sections instance");
        try {
            sessionFactory.getCurrentSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public Sections merge(Sections detachedInstance) {
        log.debug("merging Sections instance");
        try {
            Sections result = (Sections) sessionFactory.getCurrentSession().merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public Sections findById(Integer id) {
        log.debug("getting Sections instance with id: " + id);
        try {
            Sections instance = (Sections) sessionFactory.getCurrentSession().get("dto.Sections", id);
            if (instance == null) {
                log.debug("get successful, no instance found");
            } else {
                log.debug("get successful, instance found");
            }
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List<Sections> findByExample(Sections instance) {
        log.debug("finding Sections instance by example");
        try {
            List<Sections> results = (List<Sections>) sessionFactory.getCurrentSession().createCriteria("dto.Sections").add(
                    create(instance)).list();
            log.debug("find by example successful, result size: "
                    + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    public List getAllSections() {
        List results = sessionFactory.getCurrentSession().createCriteria(Sections.class).add(
                Restrictions.isNull("sections")).list();

        return results;
    }

    public Sections getSection(Sections section) // changed from void to Sections
    {
//        session.beginTransaction().begin();

        Query hqlQuery = sessionFactory.getCurrentSession().createQuery("from Sections where sections is not null");
        List result = hqlQuery.list();
        Iterator next = result.iterator();

        while (next.hasNext()) {
            Sections sec = ((Sections) next.next());
            Integer sectionId = sec.getSectionid();

            if (sectionId.equals(section.getSectionid())) {
                section = sec;
            }
        }

        return section;
    }

    public void getParentSection() {
//        session.beginTransaction().begin();

        Query hqlQuery = sessionFactory.getCurrentSession().createQuery("from Sections where sections is null");
        List result = hqlQuery.list();
        Iterator next = result.iterator();

        while (next.hasNext()) {
            mainForum = (Sections) next.next();
        }
    }

    public void getInsideSection(Sections section) {

        Query hqlQuery = sessionFactory.getCurrentSession().createQuery("from Sections");
        List result = hqlQuery.list();
        Iterator next = result.iterator();

        while (next.hasNext()) {
            Sections sec = ((Sections) next.next());
            Sections childSec = sec.getSections();

            if (childSec != null) {
                Integer sectionId = childSec.getSectionid();

                if (sectionId.equals(section.getSectionid())) {
                    innerSections.addElement(sec);
                }
            }
        }

        Query hqlQuery1 = sessionFactory.getCurrentSession().createQuery("from Topics order by creationdate desc");
        List result1 = hqlQuery1.list();
        Iterator next1 = result1.iterator();

        while (next1.hasNext()) {
            Topics topic = ((Topics) next1.next());
            Sections topicSection = topic.getSections();
            Integer secId = topicSection.getSectionid();

            if (secId.equals(section.getSectionid())) {
                innerTopics.addElement(topic);
            }
        }
    }

    public Sections getMainForum() {
        return mainForum;
    }

    public void setMainForum(Sections mainForum) {
        this.mainForum = mainForum;
    }

    public Vector<Sections> getInnerSections() {
        return innerSections;
    }

    public void setInnerSections(Vector<Sections> innerSections) {
        this.innerSections = innerSections;
    }

    public Vector<Topics> getInnerTopics() {
        return innerTopics;
    }

    public void setInnerTopics(Vector<Topics> innerTopics) {
        this.innerTopics = innerTopics;
    }

    public List getSections() {
        List results = sessionFactory.getCurrentSession().createCriteria(Sections.class).list();

        return results;
    }
	//////////////////////////////////////////////////////Admin////////////////////////////////////
	
	 public List getForums() {
        List results = sessionFactory.getCurrentSession().createCriteria(Sections.class).add(
                           Restrictions.isNull("sections")).list();

        return results;
    }

    public void startNew() {
        sessionFactory.getCurrentSession().beginTransaction();;
    }

    public void finish() {
        sessionFactory.getCurrentSession().close();
    }

    public boolean commitTrans() {
        boolean result = false;

        try {
            sessionFactory.getCurrentSession().getTransaction().commit();
            result = true;
        } catch (Exception e) {            
            result = false;
        }

        return result;
    }


    public Vector<Sections> getAllSections1() {
		Vector<Sections> sections = new Vector<Sections>();
		Sections sec = new Sections();
                sessionFactory.getCurrentSession().beginTransaction();
	    Query q = sessionFactory.getCurrentSession().createQuery("from Sections");
		Iterator  it = q.list().iterator();
	    while (it.hasNext()) {
			sec =(Sections)it.next();
		    sections.add(sec);
		}
sessionFactory.getCurrentSession().close();
		return sections;
	}

 public List getAllThemes() {
    	List results=sessionFactory.getCurrentSession().createCriteria(Themes.class).addOrder(Order.asc("themename")).list();
    	return results;
	}

}
